package com.gxsf.wechatbook.controller;

import com.gxsf.wechatbook.pojo.CheckList;
import com.gxsf.wechatbook.pojo.ReserList;
import com.gxsf.wechatbook.pojo.ReserUserInfo;
import com.gxsf.wechatbook.pojo.User;
import com.gxsf.wechatbook.repo.CheckListRepository;
import com.gxsf.wechatbook.repo.ReserListRepository;
import com.gxsf.wechatbook.repo.ReserUserInfoRepository;
import com.gxsf.wechatbook.repo.UserRepository;
import com.gxsf.wechatbook.token.UserLoginToken;
import com.gxsf.wechatbook.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;

import com.gxsf.wechatbook.util.Error;

/**
 * @author BeijiRonin
 * @version 1.0.0
 * @date 2021/1/25 9:47
 */
@RestController
@RequestMapping("/check_json")
@Api("check_json 签到接口")
@CrossOrigin
public class CheckController {
    @Autowired
    CheckListRepository checkListRepository;
    @Autowired
    UserRepository userRepository;

    @Autowired
    private ReserListRepository reserListRepository;

    @Autowired
    private ReserUserInfoRepository reserUserInfoRepository;

    @UserLoginToken
    @ApiOperation("今天的签到人数")
    @PostMapping("/dayCheck.json")
    public List<CheckList> dayCheck() {
        Timestamp timestamp = new Timestamp(DateUtil.get8Date().getTime());
        List<CheckList> allByNowDay = this.checkListRepository.findAllByNowDay(timestamp);
        return allByNowDay;
    }


    @UserLoginToken
    @ApiOperation("积分签到")
    @PostMapping("/jfcheck.opera/{userId}")
    public Object jfcheck(@PathVariable("userId") long userId) {
        List<CheckList> allByNowDay = checkListRepository.findAllByUserIdOrderByCreateTimeDesc(userId);
        Iterator var4 = allByNowDay.iterator();

        for (CheckList checkList : allByNowDay) {
            if (checkList.getUserId() == userId) {
                return Error.error(500, "你今天已经签到了");
            }
        }
        CheckList checkList = new CheckList();
        checkList.setCreateTime(new Timestamp(System.currentTimeMillis()));
        checkList.setUserId(userId);

        Optional<User> byId = userRepository.findById(userId);
        if (byId.isPresent()) {
            User user = byId.get();
            user.setScope(user.getScope() + 5);
            userRepository.save(user);
        }

        return checkListRepository.save(checkList);
    }


    @UserLoginToken
    @ApiOperation("预约签到")
    @PostMapping("/check.opera/{userId}")
    public Object check(@PathVariable("userId") long userId) {
        //查询预约记录
        ReserUserInfo reserUserInfo = reserUserInfoRepository.findFirstByUserIdAndStatusOrderById(userId, 0);
        if (ObjectUtils.isEmpty(reserUserInfo)) {
            return Error.error(500, "无预约记录！请预约再签到");
        }

        //查询图书馆位置是否满足
        ReserList reserList = reserListRepository.findLibraryById(reserUserInfo.getLibraryId());
        //创建签到记录
        ReserList checkrecord = reserListRepository.save(reserList);
        //插入签到表成功，

        Optional<User> byId = userRepository.findById(userId);
        if (byId.isPresent()) {
            User user = byId.get();
            user.setScope(user.getScope() + 5);
            userRepository.save(user);
        }

        //更新预约状态
        reserUserInfo.setStatus(1);
        if (!ObjectUtils.isEmpty(checkrecord)) {
            reserList.setNowPeople(reserList.getNowPeople() + 1);
            reserListRepository.update(reserList);

        }
        return checkrecord;
    }

    @UserLoginToken
    @ApiOperation("签退")
    @PostMapping("/uncheck.opera/{userId}")
    public Object uncheck(@PathVariable("userId") long userId) {
        //查询预约表
        ReserUserInfo reserUserInfo = reserUserInfoRepository.findFirstByUserIdAndStatusOrderById(userId, 1);
        if (ObjectUtils.isEmpty(reserUserInfo)) {
            return Error.error(500, "没有签到记录！");
        }
        //更新预约状态
        reserUserInfo.setStatus(2);
        //更新预约时间
        reserUserInfo.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        ReserList reserList = reserListRepository.findLibraryById(reserUserInfo.getLibraryId());
        //更新图书馆位置数量
        reserList.setNowPeople(reserList.getNowPeople() - 1);
        reserListRepository.update(reserList);
        return reserList;

    }
}
